package com.ruoyi.project.wx.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.shop.dao.ShopShopDao;
import com.ruoyi.project.shop.domain.ShopShopDO;
import com.ruoyi.project.wx.domain.ShopExtractDO;
import com.ruoyi.project.wx.service.ShopExtractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商家提现Controller
 *
 * @author long
 * @date 2021-04-15
 */
@RestController
@RequestMapping("/shop/shopExtract")
public class ShopExtractController extends BaseController
{
	@Autowired
	private ShopExtractService shopExtractService;
	@Autowired
	private ShopShopDao shopShopDao;
	/**
	 * 查询商家提现列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('shop:shopExtract:list')")
	@GetMapping("/list")
	public TableDataInfo list(ShopExtractDO shopExtract)
	{
		startPage();
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(shopExtract);
		stringObjectMap.put("delFlag","0");
		stringObjectMap.put("sort","create_at");
		stringObjectMap.put("order","desc");
		if(shopExtract.getStatus()!=null){
			stringObjectMap.put("status",shopExtract.getStatus()+"");
		}
		List<ShopExtractDO> shopExtractList = shopExtractService.likeList(stringObjectMap);
		if(shopExtractList.size()>0){
			for (ShopExtractDO shopExtractDO : shopExtractList) {
				ShopShopDO shopShopDO = shopShopDao.get(shopExtractDO.getShopId());
				if(shopShopDO!=null){
					shopExtractDO.setShopId(shopShopDO.getName());
				}else{
					shopExtractDO.setShopId("");
				}
			}
		}
		return getDataTable(shopExtractList);
	}

	/**
	 * 导出商家提现列表
	 */
	@PreAuthorize("@ss.hasPermi('shop:shopExtract:export')")
	@Log(title = "商家提现", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(ShopExtractDO shopExtract)
	{
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(shopExtract);
		List<ShopExtractDO> shopExtractList = shopExtractService.likeList(stringObjectMap);
		if(shopExtractList.size()>0){
			for (ShopExtractDO shopExtractDO : shopExtractList) {
				ShopShopDO shopShopDO = shopShopDao.get(shopExtractDO.getName());
				if(shopShopDO!=null){
					shopExtractDO.setShopId(shopShopDO.getName());
				}else{
					shopExtractDO.setShopId("");
				}
			}
		}
		ExcelUtil<ShopExtractDO> util = new ExcelUtil<ShopExtractDO>(ShopExtractDO.class);
		return util.exportExcel(shopExtractList, "shopExtractDO");
	}

	/**
	 * 获取商家提现详细信息
	 */
	@PreAuthorize("@ss.hasPermi('shop:shopExtract:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") String id)
	{
		return AjaxResult.success(shopExtractService.get(id));
	}

	/**
	 * 新增商家提现
	 */
	@PreAuthorize("@ss.hasPermi('shop:shopExtract:add')")
	@Log(title = "商家提现", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody ShopExtractDO shopExtract)
	{
		shopExtract.setId(IdUtil.simpleUUID());
		shopExtract.setDelFlag(0);
		shopExtract.setCreateAt(new Date());
		return toAjax(shopExtractService.save(shopExtract));
	}

	/**
	 * 修改商家提现
	 */
	@PreAuthorize("@ss.hasPermi('shop:shopExtract:edit')")
	@Log(title = "商家提现", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody ShopExtractDO shopExtract)
	{
		shopExtract.setUpdateAt(new Date());
		return toAjax(shopExtractService.update(shopExtract));
	}

	/**
	 * 删除商家提现
	 */
	@PreAuthorize("@ss.hasPermi('shop:shopExtract:remove')")
	@Log(title = "商家提现", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable String[] ids)
	{
		if(ids.length>0){
			for (String string : ids) {
				ShopExtractDO shopExtract = shopExtractService.get(string);
				shopExtract.setDelFlag(1);
				shopExtract.setUpdateAt(new Date());
				shopExtractService.update(shopExtract);
			}
		}
		return toAjax(ids.length);
		//return toAjax(shopExtractService.deleteShopExtractByIds(ids));
	}
	/**
	 * 审核
	 */
	@PreAuthorize("@ss.hasPermi('shop:shopExtract:edit')")
	@Log(title = "商家提现", businessType = BusinessType.UPDATE)
	@PutMapping("/shen")
	public R shen(ShopExtractDO shopExtract){

		return shopExtractService.shen(shopExtract);
	}
}
